package com.deloitte.snap.service;

import java.util.Date;
import java.util.List;

import com.deloitte.snap.dao.DocumentDetailDAOImpl;
import com.deloitte.snap.util.CommonUtil;
import com.deloitte.snap.util.FileUploadController;
import com.deloitte.snap.vo.DocumentDetailVO;

public class DocumentDetailService extends AbstractService{
	/**
	 * Doc Type bean name in WEB-INF/dataAccessContext.xml
	 */
	private final String DAO_NAME = "documentDetaiDAO";
	/**
	 * Instance of DAO Class
	 */
	private final DocumentDetailDAOImpl documentDetaiDAO = (DocumentDetailDAOImpl) getDAO(DAO_NAME);
	
	/**
	 * This method used to retrieve all different categories of document available in DB 
	 * 
	 * @return - Return the collection of document categories
	 */
	public List<DocumentDetailVO> findAllDocumentDetails(){
		List<DocumentDetailVO> documentDetais =  null;
		if(documentDetaiDAO != null){
			documentDetais = documentDetaiDAO.findAll();
		}
		return documentDetais;
	}
	
	public String saveOrUpdate(DocumentDetailVO documentDetailVO){
		if(documentDetaiDAO != null){
			if(CommonUtil.isEmpty(documentDetailVO.getDocDetailId())){
				String docDetailId = CommonUtil.getCurrentTime();
				documentDetailVO.setDocDetailId(docDetailId);
			}
			documentDetaiDAO.saveOrUpdate(documentDetailVO);
		}
		return documentDetailVO.getDocDetailId();
	}
	
	public void deleteDocumentDetails(DocumentDetailVO documentDetailVO){
		if(documentDetaiDAO != null){
			documentDetaiDAO.delete(documentDetailVO);
			if(!CommonUtil.isEmpty(documentDetailVO.getDocLocation())){
				FileUploadController.deleteFile(documentDetailVO.getDocLocation());
			}
		}
	}
	
	public List<DocumentDetailVO> findDocumentsForProjectScope(){
		List<DocumentDetailVO> documentDetais =  null;
		if(documentDetaiDAO != null){
			String deliverableCode = getProperty("delcodeproscope");
			documentDetais = documentDetaiDAO.findDocumentsForDeliverableCode(deliverableCode);
		}
		return documentDetais;
	}
	
}
